package com.hctforgreen.greenservice.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.hctforgreen.greenservice.utils.HctConstants;

/**
 * 终端培训：考试
 * 
 * @author 390694
 * 
 */
public class DbTerminalExamAdapter {
	private static final String DATABASE_NAME = HctConstants.DB_TERMINAL_EXAM_NAME;

	private static final int DBVERSION = HctConstants.DB_TERMINAL_EXAM_VERSION;
	private Context mContext;
	private DBHelper mHelper;
	private SQLiteDatabase mDatabase;
	/**
	 * 试题
	 */
	private static final String CREATE_KNOWLEDGE_TABLE_SQL = "create table "
			+ HctConstants.DB_TABLE_NAME_TERMINAL_EXAMSUB_LST
			+ " (_id integer primary key autoincrement, " + "id text, "
			+ "createTime text, " + "analyze text, " + "point text, "
			+ "remark text, " + "name text," + "answer text," + "type text,"
			+ "option text," + "inputAnswer text," + "paperId text" + ");";
	/**
	 * 试卷
	 */
	private static final String CREATE_EXAM_PAPER_TABLE_SQL = "create table "
			+ HctConstants.DB_TABLE_NAME_TERMINAL_EXAM_PAPER_LST
			+ " (_id integer primary key autoincrement, " + "id text, "
			+ "createTime text, " + "name text," + "remark text,"
			+ "score text, " + "passScore text, " + "personId text, "
			+ "isPass text" + ");";

	// private static final String CREATE_EXAM_PAPER_TABLE_SQL = "create table "
	// + HctConstants.DB_TABLE_NAME_EXAM_PAPER_LST
	// + " (_id integer primary key autoincrement, " + "ID text, "
	// + "title text, " + "offeranswer text, " + "answer text, "
	// + "hint text, " + "point text, " + "qtype text, "
	// + "repType text, " + "answerNum text, " + "choiseType text" + ");";

	/**
	 * 构造函数
	 * 
	 * @param context
	 */
	public DbTerminalExamAdapter(Context context) {
		mContext = context;
		mHelper = new DBHelper(mContext);
		mDatabase = mHelper.getWritableDatabase();
	}

	private class DBHelper extends SQLiteOpenHelper {

		public DBHelper(Context context) {
			super(context, DATABASE_NAME, null, DBVERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {

			/**
			 * 创建newVersion
			 */
			db.execSQL(CREATE_KNOWLEDGE_TABLE_SQL);
			/**
			 * 创建newVersion
			 */
			db.execSQL(CREATE_EXAM_PAPER_TABLE_SQL);

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			/**
			 * drop掉oldVersion
			 */
			db.execSQL("DROP TABLE IF EXISTS "
					+ HctConstants.DB_TABLE_NAME_TERMINAL_EXAMSUB_LST);
			/**
			 * drop掉oldVersion
			 */
			db.execSQL("DROP TABLE IF EXISTS "
					+ HctConstants.DB_TABLE_NAME_TERMINAL_EXAM_PAPER_LST);
			onCreate(db);
		}
	}

	/**
	 * 执行
	 * 
	 * @param sql
	 */
	public void execSQL(String sql) {
		mDatabase.execSQL(sql);
	}

	/**
	 * 执行
	 * 
	 * @param sql
	 */
	public void execSQL(String sql, Object[] bindArgs) {
		mDatabase.execSQL(sql, bindArgs);
	}

	/**
	 * 插入
	 * 
	 * @param table
	 * @param nullColumnHack
	 * @param values
	 * @return
	 */
	public long insert(String table, String nullColumnHack, ContentValues values) {
		return mDatabase.insert(table, nullColumnHack, values);
	}

	/**
	 * 查询
	 * 
	 * @param sql
	 * @param bindArgs
	 * @return
	 */
	public Cursor rawQuery(String sql, String[] bindArgs) {
		return mDatabase.rawQuery(sql, bindArgs);
	}

	/**
	 * 关闭
	 */
	public void close() {
		mDatabase.close();
	}

	public SQLiteDatabase getDatabase() {
		return mDatabase;
	}
}
